﻿using FCT551.Base;
using FCT551.Helper;
using FCT551.Models;
using FCT551.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FCT551.ViewModels
{
    public class UserLoginViewModel : NotifyPorperChanged
    {
        private UserLoginView _userLoginView;

        public UserLoginViewModel(UserLoginView userLoginView, string name, string password)
        {
            _userLoginView = userLoginView;
            ExitCommand = new Command() { ExeAction = new Action<object>(Exitcmd) };
            LoginCommand = new Command() { ExeAction = new Action<object>(Logincmd) };
            Username = name;
            UserPassword = password;
        }

        #region 绑定方法

        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Logincmd(object obj)
        {
            try
            {
                if (Username != "" && UserPassword != "")
                {
                    ErrorMessage = "";
                    //验证用户名
                    if (Global._Users != null && Global._Users.Count > 0)
                    {
                        List<User> matchUser = Global._Users.Where(u => u.UserName == Username && u.UserPassword == UserPassword).ToList();
                        if (matchUser != null && matchUser.Count == 1)
                        {
                            //成功登录，自动关闭
                            Global._User = matchUser[0];
                            _userLoginView.DialogResult = true;
                            _userLoginView.Close();
                        }
                        else
                        {
                            ErrorMessage = "用户名或者密码错误！";
                        }
                    }
                    else
                    {
                        ErrorMessage = "未加载用户配置！";
                    }
                }
                else
                {
                    ErrorMessage = "用户名或密码为空！";
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        /// <summary>
        /// 退出方法
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Exitcmd(object obj)
        {
            _userLoginView.DialogResult = false;
            _userLoginView.Close();
        }

        #endregion 绑定方法

        #region 绑定属性

        /// <summary>
        /// 退出命令
        /// </summary>
        private Command _exit;

        public Command ExitCommand
        {
            set
            {
                _exit = value;
            }
            get
            {
                return _exit;
            }
        }

        /// <summary>
        /// 登录命令
        /// </summary>
        private Command _loginCommand;

        public Command LoginCommand
        {
            set
            {
                _loginCommand = value;
            }
            get
            {
                return _loginCommand;
            }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        private string _username = "";

        public string Username
        {
            set
            {
                _username = value;
                this.SetProperty();
            }
            get
            {
                return _username;
            }
        }

        /// <summary>
        /// 用户密码
        /// </summary>
        private string _userPassword = "1";

        public string UserPassword
        {
            set
            {
                _userPassword = value;
                this.SetProperty();
            }
            get
            {
                return _userPassword;
            }
        }

        /// <summary>
        /// 错误消息
        /// </summary>
        private string _errorMessage;

        public string ErrorMessage
        {
            set
            {
                _errorMessage = value;
                this.SetProperty();
            }
            get => _errorMessage;
        }

        #endregion 绑定属性

      
    }
}